package com.uphone.kingmall.activity.personal;

import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.PopupWindow;

import com.blankj.utilcode.util.KeyboardUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.activity.ShopDetailActivity;
import com.uphone.kingmall.adapter.AttentionStoreAdapter;
import com.uphone.kingmall.app.MyApplication;
import com.uphone.kingmall.base.BaseListActivity;
import com.uphone.kingmall.bean.UserShopCollectBean;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.utils.HttpParamsUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.PopupUtil;
import com.uphone.kingmall.utils.navigationbar.DefaultNavigationBar;

/**
 * <pre>
 *     author : 王雨
 *     e-mail :
 *     time   : 2018/07/31
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class AttentionStoreActivity extends BaseListActivity<UserShopCollectBean, UserShopCollectBean.DataBean> {
    private EditText etKeyWord;
    private PopupUtil searchPopup;

    @Override
    protected String initTitle() {
        return "关注店铺";
    }

    @Override
    protected void setTitle(final DefaultNavigationBar navigationBar) {
        ll.setBackgroundColor(Color.WHITE);

        super.setTitle(navigationBar);
        navigationBar.setRightIcon(R.mipmap.search_poi);
        navigationBar.setIvRightClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                searchPopup();

                CommonUtil.startIntent(AttentionStoreActivity.this, AttentionSearchActivity.class);
            }
        });
    }

    @Override
    protected void itemClick(BaseQuickAdapter adapter, View view, int position) {
        CommonUtil.startIntent(this, ShopDetailActivity.class, dataList.get(position).getShopId());
    }

    @Override
    protected BaseQuickAdapter initAdapter() {
        AttentionStoreAdapter adapter = new AttentionStoreAdapter();
        adapter.setOnFollowItemClickListener(new AttentionStoreAdapter.OnFollowItemClickListener() {
            @Override
            public void followClick(View view, int position, UserShopCollectBean.DataBean item) {
                unCollectShop(item.getShopId());
            }
        });
        return adapter;
    }


    private void unCollectShop(int id) {
        HttpParams params = new HttpParams();
        params.put("shopId", id);
        OkGoUtils.progressRequest(MyUrl.cancleCollectShop, this, params, new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                if (code == 0) {
                    loadData(1);
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }


    @Override
    protected UserShopCollectBean getBeans() {
        return new UserShopCollectBean();
    }

    @Override
    protected HttpParams getParams() {
        HttpParams params = HttpParamsUtils.getInstance().put("page", page)
                .put("limit", 10).getParams();
        if (!CommonUtil.checkViewEmpty(etKeyWord)) {
            params.put("keyword", etKeyWord.getText().toString().trim());
        }
        return params;
    }

    @Override
    protected String getUrl() {
        return MyUrl.getUserCollectShops;
    }

    private void searchPopup() {
        View inflate = View.inflate(this, R.layout.kingmall_popup_search, null);
        searchPopup = PopupUtil.getInstance(MyApplication.mContext, inflate);
        searchPopup.setOutsideTouchable(false).setFocusable(true).showTop();
        searchPopup.setDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (searchPopup != null) {
                    searchPopup.release();
                }
            }
        });
        inflate.findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (searchPopup != null) {
                    searchPopup.dismiss();
                }
                if (etKeyWord != null) {
                    etKeyWord.setText("");
                    etKeyWord.setHint("请输入您的搜索内容");
                }
            }
        });
        etKeyWord = inflate.findViewById(R.id.et_content);
        etKeyWord.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                KeyboardUtils.showSoftInput(v);
                return true;
            }
        });
        etKeyWord.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                loadData(1);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (searchPopup != null) {
            searchPopup.release();
        }
    }


//    @BindView(R.id.iv_back)
//    ImageView ivBack;
//    @BindView(R.id.iv_search)
//    ImageView ivSearch;
//    @BindView(R.id.rv_store_list)
//    RecyclerView rvStoreList;
//    @BindView(R.id.refreshLayout)
//    SmartRefreshLayout refreshLayout;
//
//    @Override
//    public int getLayoutContentId() {
//        return R.layout.activity_attention_store;
//    }
//
//    @Override
//    public void initView() {
//        setStatusBar();
//        List list=new ArrayList();
//        for(int i=0;i<4;i++){
//            list.add("");
//        }
//        rvStoreList.setLayoutManager(new LinearLayoutManager(this));
//        AttentionStoreAdapter adapter=new AttentionStoreAdapter(R.layout.item_attent_store,list);
//        rvStoreList.setAdapter(adapter);
//    }
//
//    @Override
//    public void initData() {
//    }
//
//
//    @OnClick({R.id.iv_back, R.id.iv_search})
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.iv_back:
//                onBackPressed();
//                break;
//            case R.id.iv_search:
//                openActivity(SearchAttentShopActivity.class);
//                break;
//        }
//    }
}
